package leetcode101.double_pointer;

/**
 * @author Synhard
 * @version 1.0
 * @Class Code4
 * @Description 680. Valid Palindrome II
 * Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.
 * Example 1:
 * Input: "aba"
 * Output: True
 * Example 2:
 * Input: "abca"
 * Output: True
 * Explanation: You could delete the character 'c'.
 * Note:
 * The string will only contain lowercase characters a-z. The maximum length of the string is 50000.
 * @tel 13001321080
 * @email 823436512@qq.com
 * @date 2021-03-27 14:30
 */
public class Code4 {
    public static void main(String[] args) {
        System.out.println(validPalindrome("abc"));
    }

    public static boolean validPalindrome(String s) {
        int left = 0, right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return check(s.substring(left + 1, right + 1), 0, right - left - 1)
                        || check(s.substring(left, right), 0, right - left - 1);
            } else {
                left++;
                right--;
            }
        }
        return true;
    }

    /*
    判断给定区间内该字符串是否是回文串
     */
    public static boolean check(String s, int left, int right) {
        while (left < right) {
            if (s.charAt(left) == s.charAt(right)) {
                left++;
                right--;
            } else {
                return false;
            }
        }
        return true;
    }
}
